<template>
	<view class="cart-container">
		<!-- 头部 -->
		<view class="cart-header">
			<view class="back-btn" @click="goBack">
				<text class="icon">←</text>
			</view>
			<text class="header-title">购物车</text>
			<view class="header-actions">
			</view>
		</view>
		
		<!-- 操作栏 -->
		<view class="action-bar" v-if="cartItems.length > 0">
			<view class="filter-btn">
				<text class="filter-text">筛选</text>
			</view>
			<view class="clear-btn" @click="clearCart">
				<text class="clear-icon">🗑</text>
				<text class="clear-text">清空</text>
			</view>
		</view>
		
		<!-- 购物车内容 -->
		<view class="cart-content" v-if="cartItems.length > 0">
			<view class="cart-list">
				<view 
					v-for="(item, index) in cartItems" 
					:key="index"
					class="cart-item"
				>
					<view class="item-image-placeholder">
						<text class="placeholder-icon">🍽</text>
					</view>
					<view class="item-info">
						<text class="item-name">{{ item.name }}</text>
						<text class="item-price">¥{{ item.price }}</text>
					</view>
					<view class="quantity-control">
						<view @click="decreaseQuantity(index)">
							<uni-icons type="minus-filled" size="32"></uni-icons>
						</view>
						<text class="quantity-text">{{ item.quantity }}</text>
						<view @click="increaseQuantity(index)">
							<uni-icons type="minus-plus" size="32"></uni-icons>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 空购物车状态 -->
		<view class="empty-cart" v-else>
			<view class="empty-icon">
				<text class="icon">🛒</text>
			</view>
			<text class="empty-title">购物车是空的</text>
			<text class="empty-subtitle">快去添加一些商品吧</text>
			<view class="go-shopping-btn" @click="goToMenu">
				<text class="btn-text">去点单</text>
			</view>
		</view>
		
		<!-- 底部结算栏 -->
		<view class="checkout-bar" v-if="cartItems.length > 0">
			<view class="total-info">
				<text class="total-label">合计<text class="total-price">¥{{ totalPrice }}</text> (共{{ totalQuantity }}件)</text>
			</view>
			<view class="checkout-btn" @click="checkout">
				<text class="checkout-text">结算</text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cartItems: []
			}
		},
		computed: {
			totalPrice() {
				return this.cartItems.reduce((total, item) => {
					return total + (parseFloat(item.price) * item.quantity);
				}, 0).toFixed(2);
			},
			totalQuantity() {
				return this.cartItems.reduce((total, item) => {
					return total + item.quantity;
				}, 0);
			}
		},
		onShow() {
			// 从本地存储获取购物车数据
			this.loadCartData();
		},
		methods: {
			loadCartData() {
				try {
					const cartData = uni.getStorageSync('cartItems');
					if (cartData) {
						this.cartItems = JSON.parse(cartData);
					}
				} catch (e) {
					console.error('加载购物车数据失败:', e);
				}
			},
			saveCartData() {
				try {
					uni.setStorageSync('cartItems', JSON.stringify(this.cartItems));
				} catch (e) {
					console.error('保存购物车数据失败:', e);
				}
			},
			increaseQuantity(index) {
				this.cartItems[index].quantity++;
				this.saveCartData();
			},
			decreaseQuantity(index) {
				if (this.cartItems[index].quantity > 1) {
					this.cartItems[index].quantity--;
				} else {
					this.cartItems.splice(index, 1);
				}
				this.saveCartData();
			},
			clearCart() {
				uni.showModal({
					title: '确认清空',
					content: '确定要清空购物车吗？',
					success: (res) => {
						if (res.confirm) {
							this.cartItems = [];
							this.saveCartData();
							uni.showToast({
								title: '购物车已清空',
								icon: 'success'
							});
						}
					}
				});
			},
			checkout() {
				uni.showToast({
					title: '结算功能开发中',
					icon: 'none'
				});
			},
			goBack() {
				uni.navigateBack();
			},
			goToMenu() {
				uni.reLaunch({
					url: '/pages/menu/menu'
				});
			}
		}
	}
</script>

<style scoped>
.cart-container {
	display: flex;
	flex-direction: column;
	height: 100vh;
	background-color: #f8f8f8;
}

.cart-header {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 40rpx;
	padding-top: 90rpx;
	background-color: #fff;
	border-bottom: 1rpx solid #eee;
	position: relative;
}

.back-btn {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.back-btn .icon {
	font-size: 36rpx;
	color: #333;
}

.header-title {
	font-size: 36rpx;
	font-weight: bold;
	color: #333;
}

.header-actions {
	display: flex;
	gap: 20rpx;
}

.action-btn {
	width: 60rpx;
	height: 60rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.action-btn .icon {
	font-size: 36rpx;
	color: #333;
}

.action-bar {
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 20rpx 40rpx;
	background-color: #fff;
	border-bottom: 1rpx solid #eee;
}

.filter-btn {
	padding: 10rpx 20rpx;
}

.filter-text {
	font-size: 28rpx;
	color: #999;
}

.clear-btn {
	display: flex;
	align-items: center;
	padding: 10rpx 20rpx;
}

.clear-icon {
	font-size: 36rpx;
	color: #999;
	margin-right: 10rpx;
}

.clear-text {
	font-size: 28rpx;
	color: #999;
}

.cart-content {
	flex: 1;
	overflow-y: auto;
}

.cart-list {
	padding: 20rpx;
}

.cart-item {
	display: flex;
	align-items: center;
	padding: 30rpx;
	margin-bottom: 20rpx;
	background-color: #fff;
	border-radius: 16rpx;
	box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
}

.item-image-placeholder {
	width: 120rpx;
	height: 120rpx;
	border-radius: 12rpx;
	margin-right: 30rpx;
	background-color: #f0f0f0;
	display: flex;
	align-items: center;
	justify-content: center;
}

.item-image-placeholder .placeholder-icon {
	font-size: 60rpx;
	color: #ccc;
}

.item-info {
	flex: 1;
}

.item-name {
	font-size: 32rpx;
	color: #333;
	margin-bottom: 10rpx;
	display: block;
}

.item-price {
	font-size: 36rpx;
	color: #d32f2f;
	font-weight: bold;
}

.quantity-control {
	display: flex;
	align-items: center;
	border: 1rpx solid #eee;
	border-radius: 8rpx;
	padding: 4rpx;
}

.quantity-btn {
	width: 60rpx;
	height: 60rpx;
	background-color: #f8f8f8;
	border-radius: 6rpx;
	display: flex;
	align-items: center;
	justify-content: center;
}

.quantity-btn .icon {
	font-size: 32rpx;
	color: #333;
}

.quantity-text {
	font-size: 32rpx;
	color: #333;
	font-weight: bold;
	min-width: 80rpx;
	text-align: center;
}

.empty-cart {
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	height: 100%;
	padding: 100rpx;
}

.empty-icon {
	margin-bottom: 40rpx;
}

.empty-icon .icon {
	font-size: 120rpx;
	color: #ccc;
}

.empty-title {
	font-size: 36rpx;
	color: #999;
	margin-bottom: 20rpx;
}

.empty-subtitle {
	font-size: 28rpx;
	color: #ccc;
	margin-bottom: 60rpx;
}

.go-shopping-btn {
	background-color: #d32f2f;
	padding: 20rpx 60rpx;
	border-radius: 50rpx;
}

.btn-text {
	color: #fff;
	font-size: 32rpx;
	font-weight: bold;
}

.checkout-bar {
	position: fixed;
	bottom: 0;
	left: 0;
	right: 0;
	height: 120rpx;
	background-color: #fff;
	border-top: 1rpx solid #eee;
	display: flex;
	align-items: center;
	justify-content: space-between;
	padding: 0 30rpx;
	padding-bottom: env(safe-area-inset-bottom);
}

.total-info {
	display: flex;
	align-items: center;
}

.total-label {
	font-size: 32rpx;
	color: #333;
}

.total-label .total-price {
	color: #d32f2f;
	font-weight: bold;
}

.checkout-btn {
	background-color: #d32f2f;
	padding: 20rpx 40rpx;
	border-radius: 50rpx;
}

.checkout-text {
	color: #fff;
	font-size: 32rpx;
	font-weight: bold;
}
</style> 